/* eslint-disable react/prop-types */
import React, { useState, createContext, useContext } from 'react'
// eslint-disable-next-line @typescript-eslint/explicit-module-boundary-types
//只7个组件（在结果中显示，越高越好/越低越好，总分划界分（满分：100），上面的3个配置，解读，因子，总的，）
const listContext = createContext(null)

// 在结果中显示
function Show (props) {
  const { list, setList } = useContext(listContext)
  const show = list[props.index].show

  // 因子的input改变
  const factorChange = (e) => {
    list[props.index][e.target.name] = e.target.value
    setList([...list])
  }

  return (
    <li>
      <input type='checkbox' name='show' value={show} onChange={factorChange} />在结果中显示
    </li>
  )
}

// 越高越好/越低越好
function EvaluationCriteria (props) {
  const { list, setList } = useContext(listContext)
  const evaluation_criteria = list[props.index].evaluation_criteria

  // 因子的input改变
  const factorChange = (e) => {
    list[props.index][e.target.name] = e.target.value
    setList([...list])
  }

  return (<>
    <li>
      <input type='radio' name='evaluation_criteria' value={evaluation_criteria} onChange={factorChange} />越高越好
      <input type='radio' name='evaluation_criteria' value={evaluation_criteria} onChange={factorChange} />越低越好
    </li>
  </>)
}

// 总分划界分（满分：100）
function DemarcationScore (props) {
  const { list, setList } = useContext(listContext)
  const demarcation_score = list[props.index].demarcation_score

  // 因子的input改变
  const factorChange = (e) => {
    list[props.index][e.target.name] = e.target.value
    setList([...list])
  }

  return (<>
    <li>
      <p>总分划界分（满分：100）</p>
      <input type='input' name='demarcation_score' value={demarcation_score} onChange={factorChange} />
    </li>
  </>)
}

// 上面的3个配置
function Configure (props) {
  return (
    <>
      <Show index={props.index} />
      <EvaluationCriteria index={props.index} />
      <DemarcationScore index={props.index} />
    </>
  )
}

// 解读
function Analysis (props) {
  const { list, setList } = useContext(listContext)
  const analysisList = list[props.index].analysis

  // // 解读input改变的时候
  const analysisChange = (e, i) => {
    list[props.index].analysis[i][e.target.name] = e.target.value
    setList([...list])
  }

  // 新增一条解读
  const addAnalysis = () => {
    list[props.index].analysis.push({ code: Math.random() * 1000, min: 0, max: 0, content: '' })
    setList([...list])
  }

  return (<>
    <li>解读：</li>
    {
      analysisList.map((val, i) => {
        return <li key={val.code}>
          <p>当（得分-划界分）在</p>
          <input type='input' name='min' value={val.min} onChange={(e) => analysisChange(e, i)} />～
          <input type='input' name='max' value={val.max} onChange={(e) => analysisChange(e, i)} />
          <p>之间，则显示</p>
          <textarea rows="3" cols="20" name='content' value={val.content} onChange={(e) => analysisChange(e, i)} />
        </li>
      })
    }
    <button onClick={addAnalysis}>新增一条解读</button>
  </>)
}

// 因子
function Factor (props) {
  const { list } = useContext(listContext)
  const factor = list[props.index]

  return (
    <ul>
      <h2>{factor.name}</h2>
      <Configure index={props.index} />
      <Analysis index={props.index} />
    </ul>
  )
}

// 总的
// eslint-disable-next-line @typescript-eslint/explicit-module-boundary-types
function Plan3 () {
  const [list, setList] = useState([
    {
      factorid: 2718974317,
      name: '因子1',
      show: 1,
      evaluation_criteria: 1,
      demarcation_score: 60,
      analysis: [{ code: 1212, min: 20, max: 30, content: '和欧俄劈腿业务1' },
        { code: 12121, min: 30, max: 40, content: '和欧俄劈腿业务2' }]
    },
    {
      factorid: 271897431789,
      name: '因子2',
      show: 0,
      evaluation_criteria: 0,
      demarcation_score: 60,
      analysis: [{ code: 122, min: 20, max: 30, content: '和欧俄1' },
        { code: 121, min: 30, max: 40, content: 'ewr务2' }]
    }
  ])

  // 提交
  const submit = () => {
    console.log('submit', list)
  }

  return (
    <listContext.Provider value={{ list, setList }}>
      <div>
        {
          list.map((item, index) => {
            return <Factor key={item.factorid} index={index} />
          })
        }
        <button onClick={submit} >提交</button>
      </div>
    </listContext.Provider>
  )
}

export default Plan3